package com.newland.mesdk.demo.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.storage.ReadFileResult;
import com.newland.mtype.module.common.storage.StorageResult;
import com.newland.mtype.module.common.storage.WriteFileResult;
import com.newland.mtype.util.Dump;
import com.newland.mtype.util.ISOUtils;
import com.newland.mesdk.demo.interfaceimpl.StorageInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.SharedPreferencesUtil;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 存储模块
 */
public class StorgaeFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private String connectType;
	private StorageInterfaceImpl storageInterfaceImpl;
	private Button btnInit, btnGetRecordNum, btnAddRecord, btnUpdat;
	private Button btnGetRecord, btnWriteFile, btnReadFile;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_storage, null);
		mainActivity = (MainActivity) getActivity();
		connectType = SharedPreferencesUtil.getStringParam(view.getContext(), Const.CONNECTTYPE);
		initStorageView(view);
		return view;
	}

	public void initStorageView(View view) {
		btnInit = (Button) view.findViewById(R.id.btn_storage_init);
		btnInit.setOnClickListener(this);

		btnGetRecordNum = (Button) view.findViewById(R.id.btn_storage_get_record_num);
		btnGetRecordNum.setOnClickListener(this);

		btnAddRecord = (Button) view.findViewById(R.id.btn_storage_addrecord);
		btnAddRecord.setOnClickListener(this);

		btnUpdat = (Button) view.findViewById(R.id.btn_storage_updata);
		btnUpdat.setOnClickListener(this);

		btnGetRecord = (Button) view.findViewById(R.id.btn_storage_get_record);
		btnGetRecord.setOnClickListener(this);

		btnWriteFile = (Button) view.findViewById(R.id.btn_storage_write_file);
		btnWriteFile.setOnClickListener(this);

		btnReadFile = (Button) view.findViewById(R.id.btn_storage_read_file);
		btnReadFile.setOnClickListener(this);

		if (connectType.equals(Const.ConnectTypeName.K21) || connectType.equals(Const.ConnectTypeName.AUDIO)) {
			btnWriteFile.setVisibility(View.GONE);
			btnReadFile.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				storageInterfaceImpl = new StorageInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_storage_init:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始初始化存储记录"+ "\r\n", MessageTag.TIP);
								boolean issuccess = storageInterfaceImpl.initializeRecord("FLOWLISTS", 256, 10, 20, 110, 20);
								if (issuccess) {
									mainActivity.showMessage("初始化存储记录成功!" + "\r\n", MessageTag.NORMAL);
									mainActivity.processingUnLock();
								} else {
									mainActivity.showMessage("初始化存储记录失败!" + "\r\n", MessageTag.ERROR);
									mainActivity.processingUnLock();
								}

							} catch (Exception ex) {
								mainActivity.showMessage("初始化存储记录失败!" + ex + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_storage_get_record_num:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始获取存储记录数"+ "\r\n", MessageTag.TIP);
								int count = storageInterfaceImpl.fetchRecordCount("FLOWLISTS");
								mainActivity.showMessage("获取存储记录数：" + count + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception ex) {
								mainActivity.showMessage("获取存储记录数失败!" + ex + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_storage_addrecord:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								// 检索偏移量从0开始
								// 该范例检索字段1偏移量10，长度5，则检索字段为00001
								// 该范例检索字段2偏移量16，长度10，则检索字段为00001
								mainActivity.processingLock();
								mainActivity.showMessage("开始增加存储记录"+ "\r\n", MessageTag.TIP);
								StorageResult storageResult = storageInterfaceImpl.addRecord("FLOWLISTS", "银联支付1|00001|6666666666".getBytes("gbk"));
								mainActivity.showMessage(" 增加存储记录结果：" + StorageResult.getResultDesc(storageResult.getResultCode()) + "\r\n",
										MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception ex) {
								mainActivity.showMessage("增加存储记录失败，请确保文件存在!" + ex + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_storage_get_record:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始获取存储记录"+ "\r\n", MessageTag.TIP);
								byte[] record = storageInterfaceImpl.fetchRecord("FLOWLISTS", 1, "银联", "66");
								if (record == null) {
									mainActivity.showMessage("没有相应的存储记录可获取" + "\r\n", MessageTag.ERROR);
									mainActivity.showMessage("请先增加存储记录" + "\r\n", MessageTag.TIP);
									mainActivity.processingUnLock();
								} else {
									mainActivity.showMessage("取到存储记录为：" + Dump.getHexDump(record) + "\r\n", MessageTag.DATA);
									mainActivity.processingUnLock();
								}

							} catch (Exception e) {
								mainActivity.showMessage("获取存储记录失败!" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_storage_updata:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始更新存储记录"+ "\r\n", MessageTag.TIP);
								StorageResult storageResult1 = storageInterfaceImpl.updateRecord("FLOWLISTS", 1, "银联", "2014", "信用卡".getBytes());
								mainActivity.showMessage("更新存储记录结果：" + StorageResult.getResultDesc(storageResult1.getResultCode()) + "\r\n",
										MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception ex) {
								mainActivity.showMessage("更新存储记录失败!是否已经增加了存储记录！" + ex + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();
					break;
				case R.id.btn_storage_read_file:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始读文件"+ "\r\n", MessageTag.TIP);
								ReadFileResult readFileResult = storageInterfaceImpl.readFile("RECORD", 0, 10);
								mainActivity.showMessage("读文件的内容结果：" + readFileResult.getResultCode() + "\r\n", MessageTag.DATA);
								byte[] readResult = readFileResult.getContent();
								mainActivity.showMessage("读文件的内容：" + Dump.getHexDump(readResult) + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("读文件失败!" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();
					break;

				case R.id.btn_storage_write_file:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始写文件"+ "\r\n", MessageTag.TIP);
								WriteFileResult writeFileResult = storageInterfaceImpl.writeFile("RECORD", 0, "写文件1：12345千万人改变".getBytes("gbk"));
								mainActivity.showMessage("写文件结果：" + writeFileResult.getResultCode() + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("写文件失败!" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.NORMAL);
		}

	}

	

}
